//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Data;
using DotNetNuke.Entities.Users;
using System.Collections;
namespace DotNetNuke.Entities.Modules
{
	public class ModuleControlController
	{
		private static string key = "ModuleControlID";
		private static DataProvider dataProvider = DataProvider.Instance();
		private static Dictionary<int, ModuleControlInfo> GetModuleControls()
		{
			return CBO.GetCachedObject<Dictionary<int, ModuleControlInfo>>(new CacheItemArgs(DataCache.ModuleControlsCacheKey, DataCache.ModuleControlsCacheTimeOut, DataCache.ModuleControlsCachePriority), GetModuleControlsCallBack);
		}
		private static object GetModuleControlsCallBack(CacheItemArgs cacheItemArgs)
		{
			return CBO.FillDictionary<int, ModuleControlInfo>(key, dataProvider.GetModuleControls(), new Dictionary<int, ModuleControlInfo>());
		}
		public static void AddModuleControl(ModuleControlInfo objModuleControl)
		{
			SaveModuleControl(objModuleControl, true);
		}
		public static void DeleteModuleControl(int moduleControlID)
		{
			dataProvider.DeleteModuleControl(moduleControlID);
			DataCache.ClearHostCache(true);
		}
		public static ModuleControlInfo GetModuleControl(int moduleControlID)
		{
			ModuleControlInfo moduleControl = null;
			Dictionary<int, ModuleControlInfo> moduleControls = GetModuleControls();
			if (!moduleControls.TryGetValue(moduleControlID, out moduleControl)) {
				moduleControl = CBO.FillObject<ModuleControlInfo>(dataProvider.GetModuleControl(moduleControlID));
			}
			return moduleControl;
		}
		public static Dictionary<string, ModuleControlInfo> GetModuleControlsByModuleDefinitionID(int moduleDefID)
		{
			Dictionary<string, ModuleControlInfo> moduleControls = new Dictionary<string, ModuleControlInfo>();
			foreach (ModuleControlInfo moduleControl in GetModuleControls().Values) {
				if (moduleControl.ModuleDefID == moduleDefID) {
					moduleControls[moduleControl.ControlKey] = moduleControl;
				}
			}
			return moduleControls;
		}
		public static ModuleControlInfo GetModuleControlByControlKey(string controlKey, int moduleDefId)
		{
			ModuleControlInfo moduleControl = null;
			foreach (KeyValuePair<int, ModuleControlInfo> kvp in GetModuleControls()) {
				if (kvp.Value.ModuleDefID == moduleDefId && kvp.Value.ControlKey.ToLowerInvariant() == controlKey.ToLowerInvariant()) {
					moduleControl = kvp.Value;
					break;
				}
			}
			if (moduleControl == null) {
				moduleControl = CBO.FillObject<ModuleControlInfo>(dataProvider.GetModuleControlsByKey(controlKey, moduleDefId));
			}
			return moduleControl;
		}
		public static int SaveModuleControl(ModuleControlInfo moduleControl, bool clearCache)
		{
			int moduleControlID = moduleControl.ModuleControlID;
			if (moduleControlID == Null.NullInteger) {
				moduleControlID = dataProvider.AddModuleControl(moduleControl.ModuleDefID, moduleControl.ControlKey, moduleControl.ControlTitle, moduleControl.ControlSrc, moduleControl.IconFile, Convert.ToInt32(moduleControl.ControlType), moduleControl.ViewOrder, moduleControl.HelpURL, moduleControl.SupportsPartialRendering, UserController.GetCurrentUserInfo().UserID);
			} else {
				dataProvider.UpdateModuleControl(moduleControl.ModuleControlID, moduleControl.ModuleDefID, moduleControl.ControlKey, moduleControl.ControlTitle, moduleControl.ControlSrc, moduleControl.IconFile, Convert.ToInt32(moduleControl.ControlType), moduleControl.ViewOrder, moduleControl.HelpURL, moduleControl.SupportsPartialRendering,
				UserController.GetCurrentUserInfo().UserID);
			}
			if (clearCache) {
				DataCache.ClearHostCache(true);
			}
			return moduleControlID;
		}
		public static void UpdateModuleControl(ModuleControlInfo objModuleControl)
		{
			SaveModuleControl(objModuleControl, true);
		}

		#region "Obsolete"

		[Obsolete("This method replaced in DotNetNuke 5.0 by Shared method GetModuleControlsByModuleDefinitionID(Integer)")]
		public static ArrayList GetModuleControls(int moduleDefID)
		{
			ArrayList arrControls = new ArrayList();
			arrControls.AddRange(GetModuleControlsByModuleDefinitionID(moduleDefID).Values);
			return arrControls;
		}

		[Obsolete("This method replaced in DotNetNuke 5.0 by Shared method GetModuleControlByControlKey(String, Integer)")]
		public static ArrayList GetModuleControlsByKey(string controlKey, int moduleDefID)
		{
			ArrayList arrControls = new ArrayList();
			arrControls.Add(GetModuleControlByControlKey(controlKey, moduleDefID));
			return arrControls;
		}

		[Obsolete("This method replaced in DotNetNuke 5.0 by Shared method GetModuleControlByControlKey(String, Integer)")]
		public static ModuleControlInfo GetModuleControlByKeyAndSrc(int moduleDefID, string controlKey, string controlSrc)
		{
			return GetModuleControlByControlKey(controlKey, moduleDefID);
		}

		#endregion
	}
}
